package com.example.service.impl;

import com.example.dto.StuFindDto;
import com.example.dto.StuInsertDormitoryByZhiYe;
import com.example.mapper.UserMapper;
import com.example.pojo.PageBean;
import com.example.pojo.User;
import com.example.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public void addUser(User user) {
        user.setCreateTime(LocalDateTime.now());;
        user.setUpdateTime(LocalDateTime.now());
        userMapper.addUser(user);
    }

    @Override
    public User findUser(String name) {
        User user = userMapper.findUser(name);
        return user;
    }

    @Override
    public void updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(user);
    }

    @Override
    public User getUserInfo(Integer id) {
        User user = userMapper.getUserInfo(id);
        return user;
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    @Override
    public PageBean PageList(Integer pageNum, Integer pageSize,String name,String dormitoryId,String categoryName) {
        PageHelper.startPage(pageNum,pageSize);
        Page<User> page = userMapper.PageList(name,dormitoryId,categoryName);
        PageBean pageBean = new PageBean();
        pageBean.setTotal(page.getTotal());
        pageBean.setItems(page.getResult());
        return pageBean;
    }

    @Override
    public void addDormitory(StuInsertDormitoryByZhiYe stuInsertDormitoryByZhiYe) {


        User user = userMapper.getUserInfo(stuInsertDormitoryByZhiYe.getId());
        String name = user.getName();
        String dormitoryId = stuInsertDormitoryByZhiYe.getDormitoryId();
        String categoryName = stuInsertDormitoryByZhiYe.getCategoryName();
        userMapper.addDormitory(name,dormitoryId,categoryName);
    }

    @Override
    public User findStu(StuFindDto stuFindDto) {
        User user = userMapper.findStu(stuFindDto);
        return user;
    }

    @Override
    public PageBean findPeo(Integer pageNum, Integer pageSize, String dormitoryId, String categoryName) {
        PageHelper.startPage(pageNum,pageSize);
        PageBean pageBean = new PageBean();
        Page<User> page = userMapper.findPeo(dormitoryId,categoryName);
        System.out.println(page);

        pageBean.setTotal(page.getTotal());
        pageBean.setItems(page.getResult());
        return pageBean;
    }

    @Override
    public User findByStuName(String stuName) {
        return userMapper.findByStuName(stuName);
    }
}
